<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 18-12-3
 * Time: 上午11:11
 */

namespace App\Services;

class TreeService {
    protected $listarr = array(), $ret,$route,$ext_html;
    protected static $level;

    public function getList($arr,$list=TRUE,$route='purview',$ext_html=''){
        $this->listarr = $arr;
        $this->ret = '';
        $this->route = $route;
        $this->ext_html = $ext_html;
        self::$level = '';
        if($list){
            $html = $this->getTree(0,$this->ret);
        }else{
            $html = $this->getOptTree(0,$this->ret);
        }
        return $html;
    }
    public function getTree($pid,$str){
        $child = $this->getChild($pid);
        if(is_array($child)){
            $this->ret .= '<ol class="dd-list">';
            foreach($child as $key => $item){
                $this->ret .= '<li class="dd-item dd3-item" data-id="'.$item['id'].'">
                                <div class="dd-handle dd3-handle"></div>
                                <div class="dd3-content">'.$item['title'];

                $this->ret .= '<div class="btn-group" style="float:right;position: relative;top:-5px;border:none;right:-10px;">
                                        <a class="btn" href="#"><i class="icon-cog"></i> 操作</a><a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="icon-caret-down"></span>
                                        </a>
                                        <ul class="dropdown-menu">
                                            <li><a href="javascript:;" onclick="show_info('.$item['id'].');"><i class="icon-eye-open"></i> 查看</a></li>
                                            <li><a href="'.route($this->route.'.edit',$item['id']).'"><i class="icon-pencil"></i> 编辑</a></li>
                                            <li><a href="javascript:;" onclick="del_forever($(this),\''.route($this->route.'.destroy',$item['id']).'\');"><i class="icon-remove"></i> 删除</a></li>';

                if($this->ext_html == 'role'){
                    $this->ret .= '<li><a href="'.route($this->route.'.assign',$item['id']).'"><i class="icon-trash"></i> 权限分配</a></li>';
                }
                                        $this->ret .= '</ul>
                                    </div>';
                if($item['status'] == 0){
                    $this->ret .= '<span class="label label-important" style="float:right;">'.$item['status_name'].'</span>';
                }else{
                    $this->ret .= '<span class="label label-info" style="float:right;">'.$item['status_name'].'</span>';
                }
                $this->ret .= '</div>
                            ';
                $this->getTree($item['id'],$this->ret);
            }
            $this->ret .= '</ol></li>';
        }else{
            $this->ret .= '</li>';
        }
        return $this->ret;
    }
    public function getOptTree($pid,$str){
        $child = $this->getChild($pid);

        if(is_array($child)){
            self::$level .= '--';
            foreach($child as $key => $item){
                $this->ret .= '<option value="'.$item['id'].'">'.self::$level.$item['title'].'</option>';
                $this->getOptTree($item['id'],$this->ret);
            }
            self::$level = "--";
        }
        return $this->ret;
    }
    private function getChild($pid){
        $newarr = array();
        foreach($this->listarr as $key => $item){
            $parent = $item['parent'] ?? 0;
            if($parent==$pid){
                $newarr[] = $item;
            }
        }
        return $newarr?$newarr:false;
    }
}
